﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private IList<IPInfo> GetIPInfo()
        {
            IList<IPInfo> rIPList = new List<IPInfo>();

            NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface NetworkIntf in NetworkInterfaces)
            {

                IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties();

                UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;

                foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
                {

                    if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        string IP = UnicastIPAddressInformation.Address.ToString();
                        if (IP != "127.0.0.1")
                        {
                            IPInfo info = new IPInfo();
                            info.IPAddress = IP;
                            info.OptStatus = NetworkIntf.OperationalStatus;
                            info.DisplayCaption = info.IPAddress;
                            if (info.OptStatus == OperationalStatus.Up)
                            {
                                info.DisplayCaption = info.DisplayCaption + " [已连接]";
                            }
                            else
                            {
                                info.DisplayCaption = info.DisplayCaption + " [已断开]";
                            }
                            rIPList.Add(info);
                        }
                    }

                }

            }

            return rIPList;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //获取所有网卡信息
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                //判断是否为以太网卡
                //Wireless80211         无线网卡;    Ppp     宽带连接;Ethernet              以太网卡
                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    //获取以太网卡网络接口信息
                    IPInterfaceProperties ip = adapter.GetIPProperties();
                    //获取单播地址集
                    UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
                    foreach (UnicastIPAddressInformation ipadd in ipCollection)
                    {
                        //InterNetwork    IPV4地址      InterNetworkV6        IPV6地址
                        //Max            MAX 位址
                        if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
                            label1.Text = ipadd.Address.ToString();//获取ip
                    }
                }
                else if (adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    //获取以太网卡网络接口信息
                    IPInterfaceProperties ip = adapter.GetIPProperties();
                    //获取单播地址集
                    UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
                    foreach (UnicastIPAddressInformation ipadd in ipCollection)
                    {
                        //InterNetwork    IPV4地址      InterNetworkV6        IPV6地址
                        //Max            MAX 位址
                        if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
                            label2.Text = ipadd.Address.ToString();//获取ip
                    }
                }
            }   
        }

        private void button2_Click(object sender, EventArgs e)
        {
            NetworkInterface[] interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
            int len = interfaces.Length;
            string mip;

            for (int i = 0; i < len; i++)
            {
                NetworkInterface ni = interfaces[i];

                if (ni.Name == "本地连接")
                {
                    IPInterfaceProperties property = ni.GetIPProperties();
                    foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            mip = ip.Address.ToString();
                            label1.Text = ip.Address.ToString();
                        }
                    }
                }
                else if (ni.Name == "本地连接2")
                {
                    IPInterfaceProperties property = ni.GetIPProperties();
                    foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            mip = ip.Address.ToString();
                            label2.Text = ip.Address.ToString();
                        }
                    }
                }
                else if (ni.Name == "无线网络连接")
                {
                    IPInterfaceProperties property = ni.GetIPProperties();
                    foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            mip = ip.Address.ToString();
                            label3.Text = ip.Address.ToString();
                        }
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
            if (addressList.Length > 1)
            {
                label1.Text = addressList[0].ToString();
                label2.Text = addressList[1].ToString();
            }
            else
            {
                label1.Text = addressList[0].ToString();
                label2.Text = "没有可用的连接 ";
            } 
        }

        public void ShowIP()
        {
            //ipv4地址也可能不止一个
            foreach (string ip in GetLocalIpv4())
            {
                this.richTextBox1.AppendText(ip.ToString());
            }
            return;
        }
        public string[] GetLocalIpv4()
        {
            //事先不知道ip的个数，数组长度未知，因此用StringCollection储存
            try
            {
                IPAddress[] localIPs;
                localIPs = Dns.GetHostAddresses(Dns.GetHostName());
                StringCollection IpCollection = new StringCollection();
                foreach (IPAddress ip in localIPs)
                {
                    //根据AddressFamily判断是否为ipv4,如果是InterNetWork则为ipv6
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                        IpCollection.Add(ip.ToString());
                }
                string[] IpArray = new string[IpCollection.Count];
                IpCollection.CopyTo(IpArray, 0);
                return IpArray;
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
            return null;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();//可以获取全部，列在richtextbox中
            ShowIP();
        }


    }
}
